import request from '@/utils/request'

// Docker服务器管理API
export const dockerServerAPI = {
  // 获取Docker服务器列表
  getServers: (params?: any) => {
    return request({
      url: '/api/docker/servers/',
      method: 'get',
      params
    })
  },

  // 获取Docker服务器详情
  getServer: (id: number) => {
    return request({
      url: `/api/docker/servers/${id}/`,
      method: 'get'
    })
  },

  // 创建Docker服务器
  createServer: (data: any) => {
    return request({
      url: '/api/docker/servers/',
      method: 'post',
      data
    })
  },

  // 更新Docker服务器
  updateServer: (id: number, data: any) => {
    return request({
      url: `/api/docker/servers/${id}/`,
      method: 'put',
      data
    })
  },

  // 删除Docker服务器
  deleteServer: (id: number) => {
    return request({
      url: `/api/docker/servers/${id}/`,
      method: 'delete'
    })
  },

  // 测试单个服务器连接
  testConnection: (id: number) => {
    return request({
      url: `/api/docker/servers/${id}/test_connection/`,
      method: 'post'
    })
  },

  // 测试连接配置（不保存）
  testConnectionConfig: (data: any) => {
    return request({
      url: '/api/docker/servers/test_connection_config/',
      method: 'post',
      data
    })
  },

  // 测试所有服务器连接
  testAllConnections: () => {
    return request({
      url: '/api/docker/servers/test_all_connections/',
      method: 'post'
    })
  },

  // 同步服务器信息
  syncServerInfo: (id: number) => {
    return request({
      url: `/api/docker/servers/${id}/sync_info/`,
      method: 'post'
    })
  },

  // 获取统计信息
  getStatistics: () => {
    return request({
      url: '/api/docker/servers/statistics/',
      method: 'get'
    })
  }
}

// Docker容器管理API
export const dockerContainerAPI = {
  // 获取容器列表
  getContainers: (serverId: number, params?: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/`,
      method: 'get',
      params
    })
  },

  // 启动容器
  startContainer: (serverId: number, containerId: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/${containerId}/start/`,
      method: 'post'
    })
  },

  // 停止容器
  stopContainer: (serverId: number, containerId: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/${containerId}/stop/`,
      method: 'post'
    })
  },

  // 重启容器
  restartContainer: (serverId: number, containerId: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/${containerId}/restart/`,
      method: 'post'
    })
  },

  // 删除容器
  removeContainer: (serverId: number, containerId: string, force?: boolean) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/${containerId}/remove/`,
      method: 'post',
      data: { force }
    })
  },

  // 获取容器日志
  getContainerLogs: (serverId: number, containerId: string, params?: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/${containerId}/logs/`,
      method: 'get',
      params
    })
  },

  // 获取容器详细信息
  inspectContainer: (serverId: number, containerId: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/${containerId}/inspect/`,
      method: 'get'
    })
  },

  // 获取容器详情
  getContainerDetails: (serverId: number, containerId: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/${containerId}/inspect/`,
      method: 'get'
    })
  },

  // 清理停止的容器
  pruneContainers: (serverId: number) => {
    return request({
      url: `/api/docker/servers/${serverId}/containers/prune/`,
      method: 'post'
    })
  }
}

// Docker镜像管理API
export const dockerImageAPI = {
  // 获取镜像列表
  getImages: (serverId: number, params?: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/images/`,
      method: 'get',
      params
    })
  },

  // 拉取镜像
  pullImage: (serverId: number, imageName: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/images/pull/`,
      method: 'post',
      data: { image_name: imageName }
    })
  },

  // 拉取镜像（支持Harbor认证）
  pullImageWithData: (serverId: number, data: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/images/pull/`,
      method: 'post',
      data
    })
  },

  // 删除镜像
  removeImage: (serverId: number, imageId: string, force?: boolean) => {
    const encodedImageId = btoa(imageId).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
    return request({
      url: `/api/docker/servers/${serverId}/images/${encodedImageId}/remove/`,
      method: 'post',
      data: { force }
    })
  },

  // 构建镜像
  buildImage: (serverId: number, data: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/images/build/`,
      method: 'post',
      data
    })
  },

  // 给镜像打标签
  tag: (serverId: number, imageId: string, data: any) => {
    const encodedImageId = btoa(imageId).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
    return request({
      url: `/api/docker/servers/${serverId}/images/${encodedImageId}/tag/`,
      method: 'post',
      data
    })
  },

  // 导出镜像
  export: (serverId: number, imageId: string, data: any) => {
    const encodedImageId = btoa(imageId).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
    return request({
      url: `/api/docker/servers/${serverId}/images/${encodedImageId}/export/`,
      method: 'post',
      data
    })
  },

  // 从镜像运行容器
  run: (serverId: number, imageId: string, data: any) => {
    // 使用base64编码来避免URL路径问题
    const encodedImageId = btoa(imageId).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
    return request({
      url: `/api/docker/servers/${serverId}/images/${encodedImageId}/run/`,
      method: 'post',
      data
    })
  },

  // 清理镜像
  prune: (serverId: number, data: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/images/prune/`,
      method: 'post',
      data
    })
  },

  // 获取镜像详细信息
  inspect: (serverId: number, imageId: string) => {
    const encodedImageId = btoa(imageId).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
    return request({
      url: `/api/docker/servers/${serverId}/images/${encodedImageId}/inspect/`,
      method: 'get'
    })
  },

  // 下载镜像到本地
  downloadImage: (serverId: number, imageId: string) => {
    const encodedImageId = btoa(imageId).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
    return request({
      url: `/api/docker/servers/${serverId}/images/${encodedImageId}/download/`,
      method: 'post'
    })
  }
}

// Docker网络管理API
export const dockerNetworkAPI = {
  // 获取网络列表
  getNetworks: (serverId: number, params?: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/networks/`,
      method: 'get',
      params
    })
  },

  // 创建网络
  createNetwork: (serverId: number, data: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/networks/`,
      method: 'post',
      data
    })
  },

  // 删除网络
  removeNetwork: (serverId: number, networkId: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/networks/${networkId}/remove/`,
      method: 'post'
    })
  },

  // 获取网络详情
  getNetworkDetails: (serverId: number, networkId: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/networks/${networkId}/inspect/`,
      method: 'get'
    })
  }
}

// Docker数据卷管理API
export const dockerVolumeAPI = {
  // 获取数据卷列表
  getVolumes: (serverId: number, params?: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/volumes/`,
      method: 'get',
      params
    })
  },

  // 创建数据卷
  createVolume: (serverId: number, data: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/volumes/`,
      method: 'post',
      data
    })
  },

  // 删除数据卷
  removeVolume: (serverId: number, volumeName: string, force?: boolean) => {
    return request({
      url: `/api/docker/servers/${serverId}/volumes/${volumeName}/remove/`,
      method: 'post',
      data: { force }
    })
  },

  // 获取数据卷详情
  getVolumeDetails: (serverId: number, volumeName: string) => {
    return request({
      url: `/api/docker/servers/${serverId}/volumes/${volumeName}/inspect/`,
      method: 'get'
    })
  },

  // 清理未使用的数据卷
  pruneVolumes: (serverId: number) => {
    return request({
      url: `/api/docker/servers/${serverId}/volumes/cleanup/`,
      method: 'post'
    })
  }
}

// Docker镜像仓库管理API
export const dockerRegistryAPI = {
  // 获取镜像仓库列表
  getRegistries: (params?: any) => {
    return request({
      url: '/api/docker/registries/',
      method: 'get',
      params
    })
  },

  // 创建镜像仓库
  createRegistry: (data: any) => {
    return request({
      url: '/api/docker/registries/',
      method: 'post',
      data
    })
  },

  // 更新镜像仓库
  updateRegistry: (id: number, data: any) => {
    return request({
      url: `/api/docker/registries/${id}/`,
      method: 'put',
      data
    })
  },

  // 删除镜像仓库
  deleteRegistry: (id: number) => {
    return request({
      url: `/api/docker/registries/${id}/`,
      method: 'delete'
    })
  },

  // 测试仓库连接
  testRegistryConnection: (id: number) => {
    return request({
      url: `/api/docker/registries/${id}/test_connection/`,
      method: 'post'
    })
  },

  // 同步仓库
  syncRegistry: (id: number) => {
    return request({
      url: `/api/docker/registries/${id}/sync_repositories/`,
      method: 'post'
    })
  },

  // Harbor项目管理
  getHarborProjects: (id: number, params?: any) => {
    return request({
      url: `/api/docker/registries/${id}/projects/`,
      method: 'get',
      params
    })
  },

  // 创建Harbor项目
  createHarborProject: (id: number, data: any) => {
    return request({
      url: `/api/docker/registries/${id}/create_project/`,
      method: 'post',
      data
    })
  },

  // 更新Harbor项目
  updateHarborProject: (id: number, data: any) => {
    return request({
      url: `/api/docker/registries/${id}/update_project/`,
      method: 'post',
      data
    })
  },

  // 删除Harbor项目
  deleteHarborProject: (id: number, projectName: string) => {
    return request({
      url: `/api/docker/registries/${id}/delete_project/`,
      method: 'post',
      data: { project_name: projectName }
    })
  },

  // 获取Harbor仓库列表
  getHarborRepositories: (id: number, projectName: string, params?: any) => {
    return request({
      url: `/api/docker/registries/${id}/repositories/`,
      method: 'get',
      params: {
        project_name: projectName,
        ...params
      }
    })
  },

  // 获取Harbor镜像列表
  getHarborArtifacts: (id: number, projectName: string, repositoryName: string, params?: any) => {
    return request({
      url: `/api/docker/registries/${id}/artifacts/`,
      method: 'get',
      params: {
        project_name: projectName,
        repository_name: repositoryName,
        ...params
      }
    })
  },

  // 删除Harbor镜像
  deleteHarborArtifact: (id: number, projectName: string, repositoryName: string, reference: string) => {
    return request({
      url: `/api/docker/registries/${id}/delete_artifact/`,
      method: 'post',
      data: {
        project_name: projectName,
        repository_name: repositoryName,
        reference: reference
      }
    })
  },

  // 触发Harbor镜像漏洞扫描
  scanHarborArtifact: (id: number, projectName: string, repositoryName: string, reference: string) => {
    return request({
      url: `/api/docker/registries/${id}/scan_artifact/`,
      method: 'post',
      data: {
        project_name: projectName,
        repository_name: repositoryName,
        reference: reference
      }
    })
  },

  // 获取Harbor镜像漏洞信息
  getHarborVulnerabilities: (id: number, projectName: string, repositoryName: string, reference: string) => {
    return request({
      url: `/api/docker/registries/${id}/vulnerabilities/`,
      method: 'get',
      params: {
        project_name: projectName,
        repository_name: repositoryName,
        reference: reference
      }
    })
  },

  // 推送镜像到Harbor
  pushImageToHarbor: (serverId: number, data: any) => {
    return request({
      url: `/api/docker/servers/${serverId}/push_to_harbor/`,
      method: 'post',
      data
    })
  },

  // 获取Harbor审计日志
  getHarborAuditLogs: (id: number, params?: any) => {
    return request({
      url: `/api/docker/registries/${id}/audit-logs/`,
      method: 'get',
      params
    })
  },

  // 获取Harbor审计日志统计
  getHarborAuditLogStatistics: (id: number) => {
    return request({
      url: `/api/docker/registries/${id}/audit-log-statistics/`,
      method: 'get'
    })
  },

  // 导出Harbor审计日志
  exportHarborAuditLogs: (id: number, params?: any) => {
    return request({
      url: `/api/docker/registries/${id}/export-audit-logs/`,
      method: 'post',
      data: params,
      responseType: 'blob'
    })
  },

  // 清理Harbor审计日志
  clearHarborAuditLogs: (id: number, days: number = 30) => {
    return request({
      url: `/api/docker/registries/${id}/clear-audit-logs/`,
      method: 'post',
      data: { days }
    })
  }
}
